package com.scm.controller;

/**
 * @author CHL
 * @ClassName: SomainController
 * @Description: TODO
 * @date 2022/3/8 14:33
 * @Version 1.0
 */
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scm.dao.SoitemMapper;
import com.scm.dao.SomainMapper;
import com.scm.entity.Soitem;
import com.scm.entity.Somain;
import com.scm.entity.response.ResponseMessage;
import com.scm.service.ISomainService;
import com.scm.util.DateUtil;
import com.scm.util.StringUtil;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.ibatis.transaction.TransactionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/main/sell"})
public class SomainController
{
    private ResponseMessage rm = new ResponseMessage();
    @Autowired
    private ISomainService somainService;
    @Autowired
    private SomainMapper somainMapper;
    @Autowired
    private SoitemMapper soitemMapper;

    public boolean checkSomain(Somain somain)
    {
        Long soId = somain.getSoId();
        if (soId == null)
        {
            this.rm.setMessage("销售单编号不能为空");
            return false;
        }
        String account = somain.getAccount();
        if (StringUtil.isEmpty(account))
        {
            this.rm.setMessage("创建用户不能为空");
            return false;
        }
        String customerCode = somain.getCustomerCode();
        if (StringUtil.isEmpty(customerCode))
        {
            this.rm.setMessage("客户编号不能为空");
            return false;
        }
        String createTime = somain.getCreateTime();
        if (StringUtil.isEmpty(createTime))
        {
            this.rm.setMessage("时间不能为空");
            return false;
        }
        if (!DateUtil.isValid(createTime))
        {
            this.rm.setMessage("时间格式不对");
            return false;
        }
        if (StringUtil.isEmpty(somain.getPayType()))
        {
            this.rm.setMessage("付款参数不能为空");
            return false;
        }
        Double productTotal = somain.getProductTotal();
        if (StringUtil.isEmpty(productTotal))
        {
            this.rm.setMessage("销售产品不能为空");
            return false;
        }
        if (productTotal.doubleValue() <= 0.0D)
        {
            this.rm.setMessage("销售产品为正数");
            return false;
        }
        Double tipFee = somain.getTipFee();
        if (tipFee.doubleValue() < 0.0D)
        {
            this.rm.setMessage("销售单附加费用不能为负数");
            return false;
        }
        Double soTotal = somain.getSoTotal();
        if (soTotal.doubleValue() != productTotal.doubleValue() + somain.getTipFee().doubleValue())
        {
            this.rm.setMessage("销售总价不正确");
            return false;
        }
        List<Soitem> soitems = somain.getSoitems();
        if ((StringUtil.isEmpty(soitems)) || (soitems.size() == 0))
        {
            this.rm.setMessage("销售详细不能为空");
            return false;
        }
        Double total = Double.valueOf(0.0D);
        for (Soitem soitem : soitems)
        {
            Integer num = Integer.valueOf(soitem.getNum());
            if (num.intValue() <= 0)
            {
                this.rm.setMessage("销售产品数量必须为正数");
                return false;
            }
            Double unitPrice = soitem.getUnitPrice();
            if (unitPrice.doubleValue() <= 0.0D)
            {
                this.rm.setMessage("销售产品单价必须为正整数");
                return false;
            }
            Double itemPrice = soitem.getItemPrice();
            double price = new BigDecimal(unitPrice.doubleValue() * num.intValue()).setScale(2, 4).doubleValue();
            if (itemPrice.doubleValue() != price)
            {
                this.rm.setMessage("销售产品明细价格不正确");
                return false;
            }
            total = Double.valueOf(total.doubleValue() + itemPrice.doubleValue());
        }
        if (productTotal.doubleValue() != total.doubleValue())
        {
            this.rm.setMessage("销售产品明细总价格不正确");
            return false;
        }
        if (soTotal.doubleValue() != productTotal.doubleValue() + tipFee.doubleValue())
        {
            this.rm.setMessage("销售单总价格不正确");
            return false;
        }
        Integer payType = somain.getPayType();
        if ((payType.intValue() != 1) && (payType.intValue() != 3) && (payType.intValue() != 2))
        {
            this.rm.setMessage("付款方式不正确");
            return false;
        }
        Double prePayFee = somain.getPrePayFee();
        if (prePayFee.doubleValue() < 0.0D)
        {
            this.rm.setMessage("预付款金额不能为负数");
            return false;
        }
        if (prePayFee.doubleValue() >= soTotal.doubleValue())
        {
            this.rm.setMessage("预付款金额金额不能超过总价格");
            return false;
        }
        Integer status = somain.getStatus();
        if ((status == null) || (status.intValue() != 1))
        {
            this.rm.setMessage("新添加销售状态应该为1表示新增");
            return false;
        }
        return true;
    }

    @RequestMapping({"/somain/add"})
    public ResponseMessage add(@RequestBody Somain somain)
    {
        if (!checkSomain(somain))
        {
            this.rm.setCode(Integer.valueOf(3));
            return this.rm;
        }
        this.somainService.insert(somain);
        this.rm.setCode(Integer.valueOf(2));
        this.rm.setMessage("新增成功");
        return this.rm;
    }

    @RequestMapping({"/somain/update"})
    public ResponseMessage update(@RequestBody Somain somain)
    {
        if (!checkSomain(somain))
        {
            this.rm.setCode(Integer.valueOf(3));
            return this.rm;
        }
        this.somainService.update(somain);
        this.rm.setCode(Integer.valueOf(2));
        this.rm.setMessage("修改成功");
        return this.rm;
    }

    @RequestMapping({"/somain/delete"})
    public ResponseMessage delete(Integer page, long soId)
    {
        page = Integer.valueOf(page == null ? 1 : page.intValue());
        this.somainService.delete(soId);
        this.rm.setCode(Integer.valueOf(2));
        this.rm.setMessage("删除成功");
        this.rm.setData(this.somainService.selectByStatus(page, 1, null));
        return this.rm;
    }

    @RequestMapping({"/somain/show"})
    public PageInfo<Somain> select(Integer page, int type, Integer payType)
    {
        PageInfo<Somain> info = this.somainService.selectByStatus(page, type, payType);
        return info;
    }

    @RequestMapping({"/somain/query"})
    public PageInfo<Somain> selectQuery(Integer page, Long soId, String customerCode, Integer payType, String startDate, String endDate, Integer status)
    {
        PageHelper.startPage(page == null ? 1 : page.intValue(), 10);
        HashMap<String, Object> m = new HashMap();
        if (!StringUtil.isEmpty(soId)) {
            m.put("soId", soId + "");
        }
        if (!StringUtil.isEmpty(customerCode)) {
            m.put("customerCode", customerCode);
        }
        if (!StringUtil.isEmpty(payType)) {
            m.put("payType", payType);
        }
        if (!StringUtil.isEmpty(startDate)) {
            m.put("startDate", startDate);
        }
        if (!StringUtil.isEmpty(endDate)) {
            m.put("endDate", endDate);
        }
        if (!StringUtil.isEmpty(status)) {
            m.put("status", status);
        }
        List<Somain> somains = this.somainMapper.selectCondition(m);
        PageInfo<Somain> info = new PageInfo(somains);

        return info;
    }

    @RequestMapping({"/somain/queryItem"})
    public List<Soitem> queryItem(Long soId)
    {
        if (soId == null) {
            throw new TransactionException("销售编号不能为空");
        }
        List<Soitem> soitems = this.soitemMapper.selectBySoId(soId.longValue());
        return soitems;
    }

    @RequestMapping({"/somain/end"})
    public ResponseMessage end(Long soId, Integer payType, Integer page, HttpServletRequest req)
    {
        String user = (String)req.getAttribute("login");
        HashMap<String, Object> map = new HashMap();
        map.put("account", user);
        map.put("soId", soId);
        map.put("now", DateUtil.currentTime());
        map.put("endStatus", Integer.valueOf(4));
        int n = this.somainMapper.end(map);
        if (n == 1)
        {
            this.rm.setCode(Integer.valueOf(2));
            this.rm.setMessage("了结成功");
            this.rm.setData(this.somainService.selectByStatus(page, 4, payType));
        }
        else
        {
            this.rm.setCode(Integer.valueOf(4));
            this.rm.setMessage("失败");
        }
        return this.rm;
    }
}

